package cn.zymall.common.utils.staticc;

import cn.zymall.common.utils.staticc.FileUtil;
import cn.zymall.common.utils.staticc.HttpUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DiscernField
 * @Description 身份证、营业执照识别静态属性
 * @Author chenmei
 * @Date 2020/8/17 16:14
 * @Version 1.0
 **/
public class DiscernField {

	// OCR webapi 接口地址
	// 应用APPID(必须为webapi类型应用,并开通营业执照识别服务,参考帖子如何创建一个webapi应用：http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=36481)
	private static final String APPID = "5f378e3b";
	// 接口密钥(webapi类型应用开通营业执照识别服务后,控制台--我的应用---营业执照识别---相应服务的apikey)
	private static final String API_KEY = "5226dfc9f8d942302c3ebe8c1eb830e8";
	// 身份证引擎类型
	//private static final String ENGINE_TYPE = "business_license";
	// 图片地址
	//private static final String AUDIO_PATH = "C://Users/chenmei/Desktop/timg.jpg";


	@RequestMapping("card")
	/**
	 *
	 * @Author: chenmei
	 * @param type	身份证值为idcard  营业执照值为:business_license）
	 * @param audioPath	身份/营业执照图片地址
	 * @Date: 2020/8/17 16:27
	 * @return:
	 **/
	public static String card(String type,String audioPath) {
		ByteArrayOutputStream outPut = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		Map<String, String> header=null;
		String result =null;
		try {
			header = buildHttpHeader(type);
			URL url = new URL(audioPath);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(10 * 1000);
			if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
				return "fail";//连接失败/链接失效/图片不存在
			}
			InputStream inStream = conn.getInputStream();
			int len = -1;
			while ((len = inStream.read(data)) != -1) {
				outPut.write(data, 0, len);
			}
			inStream.close();
			BASE64Encoder encoder = new BASE64Encoder();
			String imageBase64=encoder.encode(outPut.toByteArray());
			 result = HttpUtil.doPost1("http://webapi.xfyun.cn/v1/service/v1/ocr/"+type, header, "image=" + URLEncoder.encode(imageBase64, "UTF-8"));
		} catch (IOException r){
			System.out.println("获取身份证/营业执照信息报错");
			r.getMessage();

		}

		System.out.println("WEB card 接口调用结果：" + result);//错误码链接：https://www.xfyun.cn/document/error-code (code返回错误码时必看)400开头错误码请在接口文档底部查看
		return result;
	}

	/**
	 * 组装http请求头
	 */
	private static Map<String, String> buildHttpHeader(String type ) throws UnsupportedEncodingException {
		String curTime = System.currentTimeMillis() / 1000L + "";
		String param = "{\"engine_type\":\"" + type + "\"}";
		String paramBase64 = new String(Base64.encodeBase64(param.getBytes("UTF-8")));
		String checkSum = DigestUtils.md5Hex(API_KEY + curTime + paramBase64);
		Map<String, String> header = new HashMap<String, String>();
		header.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
		header.put("X-Param", paramBase64);
		header.put("X-CurTime", curTime);
		header.put("X-CheckSum", checkSum);
		header.put("X-Appid", APPID);
		return header;
	}
}
